package com.daydayup.learn;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo06 {

    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);

    public static void main(String[] args) throws InterruptedException {
        new Service1().service1();
    }
}
class Service1 {
    public void service1() {
        User user = new User("张三",25);
        UserContextHolder.holder.set(user);
        System.out.println("线程"+Thread.currentThread().getName()+",将用户["+ user +"]写入了共享的ThreadLocal");
        new Service2().service2();
    }
}

class Service2{
    public void service2(){
        User user = UserContextHolder.holder.get();
        System.out.println("Service2拿到了用户:"+user);
        new Service3().service3();
    }
}

class Service3{
    public void service3(){
        User user = UserContextHolder.holder.get();
        System.out.println("Service3拿到了用户:"+user);
        UserContextHolder.holder.remove();
    }
}

class UserContextHolder {
    public static ThreadLocal<User> holder = new ThreadLocal<>();
}